package edu.osu.cse551;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class ShakeMenu extends Activity implements OnClickListener {
	// private Button dmsgBtn;
	// private Button callBtn;
	private ImageButton callButton, textButton;
	int CODE;

	private String MESSAGEFILE = "message_file";
	private String PHONENOFILE = "phone_number";

	private String phone;
	private String message;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initializeMessage();
		initializePhone();
		setContentView(R.layout.alert);
		// Set up Select Contact Button
		// dmsgBtn = (Button) findViewById(R.id.defMsgBtn);
		// dmsgBtn.setOnClickListener(this);

		callButton = (ImageButton) findViewById(R.id.CallButton);
		callButton.setOnClickListener(this);

		// callBtn = (Button) findViewById(R.id.callBtn);
		// callBtn.setOnClickListener(this);

		textButton = (ImageButton) findViewById(R.id.TextButton);
		textButton.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		// case R.id.defMsgBtn:
		case R.id.TextButton:

			sendSMS(phone, message);
			moveTaskToBack(true);

			break;

		// case R.id.callBtn:
		case R.id.CallButton:
			try {
				Intent callIntent = new Intent(Intent.ACTION_CALL);
				callIntent.setData(Uri.parse("tel:" + phone));
				startActivityForResult(callIntent, CODE);
			} catch (ActivityNotFoundException e) {
				Log.e("CALL", "Call failed", e);
			}
			// moveTaskToBack(true);
			break;
		}
	}

	@Override
	public void onActivityResult(int reqCode, int resultCode, Intent data) {
		super.onActivityResult(reqCode, resultCode, data);

	}

	/*
	 * @Override public void onDestroy() { setContentView(R.layout.main);
	 * //super.onDestroy(); }
	 */

	// ---sends an SMS message to another device---
	private void sendSMS(String phoneNumber, String message) {
		String SENT = "SMS_SENT";
		String DELIVERED = "SMS_DELIVERED";

		PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
				SENT), 0);

		PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
				new Intent(DELIVERED), 0);

		// ---when the SMS has been sent---
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS sent",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(getBaseContext(), "Generic failure",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(getBaseContext(), "No service",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(getBaseContext(), "Null PDU",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(getBaseContext(), "Radio off",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(SENT));

		// ---when the SMS has been delivered---
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS delivered",
							Toast.LENGTH_SHORT).show();
					break;
				case Activity.RESULT_CANCELED:
					Toast.makeText(getBaseContext(), "SMS not delivered",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(DELIVERED));

		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
	}

	private void initializeMessage() {

		FileInputStream in = null;
		BufferedInputStream buf = null;
		try {
			in = openFileInput(MESSAGEFILE);
			buf = new BufferedInputStream(in);
		} catch (FileNotFoundException e) {
			message = "<No message>";
			return;
		}

		// read from file
		byte[] buffer = new byte[256];
		try {
			int res = buf.read(buffer, 0, 250);
			if (res == -1) {
				// file was empty
				message = "<No message>";
			} else {
				message = new String(buffer);
			}
			in.close();
		} catch (IOException e1) {
			e1.printStackTrace();
			message = "<No message>";
		}
		message = message.trim();
	}

	private void initializePhone() {

		FileInputStream in = null;
		BufferedInputStream buf = null;
		try {
			in = openFileInput(PHONENOFILE);
			buf = new BufferedInputStream(in);

		} catch (FileNotFoundException e1) {
			// File doesn't exist yet, first run
			phone = "0";
			return;
		}

		// Read from input stream
		byte[] buffer = new byte[256];
		try {
			int res = buf.read(buffer, 0, 256);
			if (res == -1) {
				// File was empty
				phone = "0";
			} else {
				phone = new String(buffer);
			}
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			phone = "0";
		}
		phone = phone.trim();
	}

}